//
//  CommonDragMsgView.swift
//  CMCCIOTSwift
//
//  Created by xukai on 2017/4/17.
//  Copyright © 2017年 isoftstone. All rights reserved.
//

import UIKit
let maskHeight : CGFloat = 80
let maskOffsetX : CGFloat = 50
enum MessageType {
    case Info
    case Error
    case Loading
    
}
class CommonDragMsgView: UIView {
   var centerView : UIView?
    var messageTips : UILabel?
    var imageTip : UIImageView?
    var activityIndicatorView : UIActivityIndicatorView?
    var type : MessageType?
    
 
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        centerView = UIView(frame: CGRect(x: maskOffsetX, y: UIScreen_Height/2-maskHeight/2, width: UIScreen_Width-2*maskOffsetX, height: maskHeight))
        self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.0)
        centerView?.backgroundColor =  UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
        centerView?.layer.cornerRadius = 5
        centerView?.clipsToBounds = true
        self .addSubview(centerView!)
        
        let gesTouch = UITapGestureRecognizer(target: self, action: #selector(touchGes))
        self.addGestureRecognizer(gesTouch)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func touchGes() {
        
        if self.type == MessageType.Loading {
            return
        }else{
            self .dismiss()
        }
        
    }
    func initUI(type : MessageType) {
        self.type = type
        switch type {
        case .Info:
            self.showInfo()
            break
        case .Error:
            self.showError()
            break
        case .Loading:
            self.startLoading()
            break
        }
    }
    
    func showInfo() {
       
        
        imageTip = UIImageView(frame: CGRect(x:((centerView?.frame.size.width)!/2 - 20/2),y:((centerView?.frame.size.height)!/2 - 20/2)-10,width:20,height:20))
        imageTip?.image = UIImage(named: "ATick");
        centerView?.addSubview(imageTip!)
        
        messageTips = UILabel(frame: CGRect(x: 0, y: ((imageTip?.frame.origin.y)!+(imageTip?.frame.size.height)!), width: (centerView?.frame.size.width)!, height: 30))
        messageTips?.font = UIFont.systemFont(ofSize: 15)
        centerView?.addSubview(messageTips!)
        messageTips?.textAlignment = .center
        messageTips?.textColor = UIColor.white
        
        
    }
    
    func showError(){
        imageTip = UIImageView(frame: CGRect(x:((centerView?.frame.size.width)!/2 - 20/2),y:((centerView?.frame.size.height)!/2 - 20/2)-10,width:20,height:20))
        imageTip?.image = UIImage(named: "Error");
        centerView?.addSubview(imageTip!)
        
        messageTips = UILabel(frame: CGRect(x: 0, y: ((imageTip?.frame.origin.y)!+(imageTip?.frame.size.height)!), width: (centerView?.frame.size.width)!, height: 30))
        messageTips?.font = UIFont.systemFont(ofSize: 15)
        centerView?.addSubview(messageTips!)
        messageTips?.textAlignment = .center
        messageTips?.textColor = UIColor.white

    }
    func startLoading(){
        
        
       activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge )
        //activityIndicatorView?.center = (centerView?.center)!
       let  point = (centerView?.center)!
        let centerY =  point.y  - (centerView?.frame.origin.y)!;
        activityIndicatorView?.center = CGPoint(x: point.x - maskOffsetX, y: centerY)
        centerView?.addSubview(activityIndicatorView!)
        activityIndicatorView?.startAnimating()
        
        messageTips = UILabel(frame: CGRect(x: 0, y: 50, width: (centerView?.frame.size.width)!, height: 20))
        messageTips?.font = UIFont.systemFont(ofSize: 13)
        centerView?.addSubview(messageTips!)
        messageTips?.textAlignment = .center
        messageTips?.textColor = UIColor.white
        
    }
    func setMessage(title:String) {
        messageTips?.text = title
        
        let time: TimeInterval = 3.0
      
        if self.type == MessageType.Info || self.type == MessageType.Error {
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time) {
                
               self.disMissMessage()
            }
        }
        
        if self.type == MessageType.Loading {
            activityIndicatorView?.activityIndicatorViewStyle = .white
            var point = activityIndicatorView?.center
            point?.y = (point?.y)! - 10;
            activityIndicatorView?.center = point!
        }
        
    }
    func stopLoading() {
        if (activityIndicatorView != nil) {
            self.activityIndicatorView?.removeFromSuperview()
            self.messageTips?.removeFromSuperview()
            self.centerView?.removeFromSuperview()
            self.removeFromSuperview()
        }
        
    }
    
    func disMissMessage() {
        
        if self.type == MessageType.Info || self.type == MessageType.Error {
        self.messageTips?.removeFromSuperview()
        self.imageTip?.removeFromSuperview()
        self.centerView?.removeFromSuperview()
        self.removeFromSuperview()
        }
    }
    
    func dismiss() {
        if self.type == MessageType.Info || self.type == MessageType.Error{
            self.disMissMessage()
        }
        if self.type == MessageType.Loading {
            self.stopLoading()
        }
    }
    

   
}
